import { constantRoutes, asyncRoutes } from '@/router'
// 路由是全局的-->路由状态维护放在vuex
const state = {
  // 开始有静态路由
  routes: [...constantRoutes]
}
// 跟新路由，静态+动态（过滤）-->用户路由
const mutations = {
  // newRoutes过滤后的动态路由
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
    // console.log('用户路由', state.routes)
  }
}
// 每条动态路由的name和menus权限点匹配
// 筛选出匹配的动态路由newRoutes
const actions = {
  filterRoutes(ctx, menus) { // menus:后端返回的权限点
    const newRoutes = asyncRoutes.filter(item => menus.includes(item.name))
    ctx.commit('setRoutes', newRoutes)
    return newRoutes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
